<template>
  <div id="insuredDetails" v-loading="isLoading">
    <InsuredIndex ref="insuredIndex" :type="2" :infoData="infoData" :pageStatus="pageStatus" @submit="handleSubmit"  @goBack="successSubmit"/>
  </div>
</template>
<script>
import InsuredIndex from './components/InsuredIndex.vue';
export default {
  name: 'insuredDetails',
  components: {
    InsuredIndex
  },
  data() {
    return {
      infoData: {},
      pageStatus: 1,
      isLoading: false,
    };
  },
  methods: {

    /**
     * @description 编辑时 获取社保档案详细数据
     */
    searchArchivesDetail(archiveId) {
      this.isLoading = true;
      this.$api.socialSecurity
        .GetArchivesDetail({ archiveId })
        .then(res => {
          console.log(res.data.data);
          const result = res.data.data;
          delete result.hireDate;
          this.infoData = {
            ...this.infoData,
            ...result
          };

          this.$nextTick(() => {
            this.$refs.insuredIndex.initPageData();
          });
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    /**
     * @description 增员
     */
    increaseStaff(data) {
      this.$api.socialSecurity
        .AddArchives(data)
        .then(res => {
          if (res.data.status == 200) {
            this.$message.success(res.data.message);
            this.successSubmit();
          } else {
            this.$message.warning(res.data.message);
          }
        });
    },

    /**
     *@description 编辑员工社保档案 
     */
    editArchives(data) {
      this.$api.socialSecurity
        .EditArchives(data)
        .then(res => {
          if (res.data.status == 200) {
            this.$message.success(res.data.message);
            this.successSubmit();
          } else {
            this.$message.warning(res.data.message);
          }
        });
    },

    /**
     * @description 新建，修改接口成功后，返回列表页并关闭当前页
     */
    successSubmit() {
      this.$store.commit('DEL_TAG', {
        value: this.$route.path,
        label: this.$route.meta.title
      });
      // this.$router.go(-1);
      this.$router.push({
        path:'/socialSecurityArchives'
      })
    },
    handleSubmit(data, isCreate) {
      console.log(data);
      isCreate ? this.increaseStaff(data) : this.editArchives(data);
    }
  },
  activated() {
    if (this.$route.params.data) {
      // console.log();
      const { departmentName, id, registerTypeDesc, realName, positionName, subId, userId, hireDate } = this.$route.params.data
      this.infoData = {
        departmentName,
        id,
        registerTypeDesc,
        realName,
        positionName,
        subId,
        userId,
        hireDate
      };
      this.pageStatus = this.$route.params.pageStatus;
      console.log(this.infoData);
      if (this.pageStatus == 3) { // 如果是编辑，就查询待编辑档案详细数据
        this.searchArchivesDetail(this.infoData.id);
        return;
      }
      ['initYear','initMonth','stopYear','stopMonth'].map(e => {
        this.infoData[e] = 0;
      })
      this.infoData.isuredRecordModel = {

      }
      // this.infoData = this.$route.params.data;
      // console.log('this.infoData', this.infoData);
      this.$nextTick(() => {
        this.$refs.insuredIndex.initPageData();
      });

    }
  }
};
</script>